﻿namespace FiftyOne.Foundation.Mobile.Detection
{
    using FiftyOne;
    using FiftyOne.Foundation.Mobile.Detection.Wurfl;
    using FiftyOne.Foundation.Mobile.Detection.Wurfl.Configuration;
    using System;
    using System.Collections;
    using System.Web;

    internal class Devices
    {
        private static Cache<IDictionary> _cache = new Cache<IDictionary>(60);

        internal static FiftyOne.Foundation.Mobile.Detection.MobileCapabilities Create(string userAgent)
        {
            IDictionary dictionary = null;
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities capabilities = null;
            if (Manager.Enabled)
            {
                if (_cache.GetTryParse(userAgent, out dictionary))
                {
                    return new FiftyOne.Foundation.Mobile.Detection.Wurfl.MobileCapabilities(dictionary);
                }
                capabilities = new FiftyOne.Foundation.Mobile.Detection.Wurfl.MobileCapabilities(userAgent);
                _cache[userAgent] = capabilities.Capabilities;
            }
            return capabilities;
        }

        internal static FiftyOne.Foundation.Mobile.Detection.MobileCapabilities Create(HttpContext context)
        {
            IDictionary dictionary = null;
            FiftyOne.Foundation.Mobile.Detection.MobileCapabilities capabilities = null;
            if (!string.IsNullOrEmpty(context.Request.UserAgent) && Manager.Enabled)
            {
                if (_cache.GetTryParse(context.Request.UserAgent, out dictionary))
                {
                    capabilities = new FiftyOne.Foundation.Mobile.Detection.Wurfl.MobileCapabilities(dictionary);
                }
                else
                {
                    capabilities = new FiftyOne.Foundation.Mobile.Detection.Wurfl.MobileCapabilities(context);
                    _cache[context.Request.UserAgent] = capabilities.Capabilities;
                }
            }
            foreach (object obj2 in context.Request.Browser.Adapters.Keys)
            {
                capabilities.Adapters.Add(obj2, context.Request.Browser.Adapters[obj2]);
            }
            return capabilities;
        }

        internal static bool IsMobileDevice(HttpContext context)
        {
            if (Manager.Enabled)
            {
                return Provider.IsMobileDevice(context);
            }
            return context.Request.Browser.IsMobileDevice;
        }
    }
}

